using System;
using System.Collections;
using System.IO;
using System.Web;
using System.Web.Mvc;
using Ext.Direct.Mvc;
using TestApplication.Code;

namespace TestApplication.Controllers
{
    public class TestController : DirectController
    {
        public ActionResult SayHello() {
            return Direct("Hello world!");
        }

        public ActionResult EchoDate(DateTime date) {
            return Direct(date);
        }

        public ActionResult AddNumbers(int a, int b) {
            return Direct(a + b);
        }

        public ActionResult EchoPerson(Person person) {
            return Direct(person);
        }

        public ActionResult LoadForm() {
            var person = new Person {
                Name = "John Smith",
                Email = "john.smith@example.com",
                Birthday = new DateTime(1969, 12, 31),
                Salary = 55000
            };

            var result = new {
                success = true,
                data = person
            };

            return Direct(result);
        }

        [FormHandler]
        public ActionResult SaveForm(Person p) {
            var result = new {
                success = true,
                person = p
            };
            return Direct(result);
        }

        [FormHandler]
        public ActionResult UploadFiles() {
            var files = Request.Files;

            foreach (string file in files) {
                HttpPostedFileBase hpf = files[file];
                if (hpf.ContentLength == 0)
                    continue;
                string folderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Uploaded");
                string savedFileName = Path.Combine(folderPath, Path.GetFileName(hpf.FileName));
                hpf.SaveAs(savedFileName);
            }

            var result = new {
                success = true
            };
            return Direct(result);
        }

        public ActionResult GetTree(string nodeId) {
            var array = new ArrayList();
            if (nodeId == "root") {
                for (int i = 0; i <= 5; i++) {
                    array.Add(new {
                        id = "n" + i,
                        text = "Node " + i,
                        leaf = false
                    });
                }
            } else if (nodeId.Length == 2) {
                var num = nodeId.Substring(1);
                for (int i = 0; i <= 5; i++) {
                    array.Add(new {
                        id = nodeId + i,
                        text = "Node " + num + i,
                        leaf = true
                    });
                }
            }
            return Direct(array.ToArray());
        }

        // API for this method will not be generated
        // because it is marked with DirectIgnoreAttribute
        [DirectIgnore]
        public ActionResult Foo() {
            ViewData["sometext"] = "Lorem ipsum dolor sit amet.";
            return View();
        }
    }
}
